package com.topone.controller;

import com.topone.Converter.ShopForm2shopApply;
import com.topone.Enum.ResultEnum;
import com.topone.Enum.ShopApplyStatusEnum;
import com.topone.Exception.FanZoneException;
import com.topone.Form.ShopApplyForm;
import com.topone.Utils.KeyUntil;
import com.topone.Utils.ResultVOUntil;
import com.topone.VO.ResultVO;
import com.topone.dataObject.ShopApply;
import com.topone.service.ShopApplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/23,0023.
 */
@CrossOrigin
@RestController
@RequestMapping("/buyer/shopApply")
@Slf4j
public class ShopApplyController {

    @Autowired
    private ShopApplyService shopApplyService;

    @GetMapping("/findOne")
    public ResultVO<Map<String,Object>> index(@RequestParam(value = "shopId" ,defaultValue = "") String shopId,
                                              Map<String,Object> map){
        ShopApply shopApply=new ShopApply();
        try {
            shopApply=shopApplyService.findOne(shopId);

        }catch (FanZoneException e){
            log.error("【查询商家申请】查询异常",e);
            return  ResultVOUntil.error(1,e.getMessage());
        }
        map.put("shopApply",shopApply);
        return ResultVOUntil.success(map);
    }

    @GetMapping("/list")
    public ResultVO list(Map<String, Object> map){

        List<ShopApply> ShopApplyList=shopApplyService.findALlInfo();
        map.put("ShopApplyList",ShopApplyList);
        return ResultVOUntil.success(map);
    }
    @GetMapping("/check")
    public ResultVO check(Map<String,Object> map,
                          @RequestParam("shopId") String shopId){
        try {
            ShopApply shopApply = shopApplyService.findOne(shopId);
            if(ShopApplyStatusEnum.WAIT.getCode()!=shopApply.getApplyStatus()){
                log.error("【商家申请入驻处理】申请状态不正确,status={}",shopApply.getApplyStatus());
                return ResultVOUntil.error(1,"【商家申请入驻处理】申请状态不正确,status={}");
            }
            shopApply.setApplyStatus(ShopApplyStatusEnum.SUCCESS.getCode());
            shopApplyService.save(shopApply);
        }catch (FanZoneException e) {
            log.error("【商家申请入驻处理】异常,e={}",e.getMessage());
            return  ResultVOUntil.error(1,e.getMessage());
        }
        log.info("【商家申请入驻处理】 入驻成功 shopId={}",shopId);
        map.put("orderId",shopId);
        return ResultVOUntil.success(map);
    }

    @GetMapping("/cancel")
    public ResultVO cancel(@RequestParam("shopId") String shopId,
                           Map<String,Object> map){
        try {
            ShopApply shopApply = shopApplyService.findOne(shopId);
            if(ShopApplyStatusEnum.WAIT.getCode()!=shopApply.getApplyStatus()){
                log.error("【商家申请取消处理】申请状态不正确,status={}",shopApply.getApplyStatus());
                return ResultVOUntil.error(1,"【商家申请取消处理】申请状态不正确");
            }
            shopApply.setApplyStatus(ShopApplyStatusEnum.fail.getCode());
            shopApplyService.save(shopApply);
        }catch (FanZoneException e) {
            log.error("【商家入驻取消处理】异常,e={}",e.getMessage());
            return ResultVOUntil.error(1,e.getMessage());
        }
        log.info("【商家入驻取消处理】 取消成功 shopId={}",shopId);
        map.put("orderId",shopId);
        map.put("msg", ResultEnum.SHOPAPPLY_CANCAL_SUCCESS.getMessage());
        return ResultVOUntil.success(map);
    }

    @PostMapping("/save")
    public ResultVO frontSave(@Valid ShopApplyForm shopApplyForm,
                              BindingResult bindingResult){
        Map<String,Object> map=new HashMap<>();
        if(bindingResult.hasErrors()){
            log.error("【商家申请入职】参数不正确,bussinessForm={}",shopApplyForm);
            map.put("error","提交参数不正确");
            return ResultVOUntil.error(1,"提交参数不正确(有为空的参数)");
        }
        ShopApply shopApply= ShopForm2shopApply.convert(shopApplyForm);
        shopApply.setShopId(KeyUntil.getUniqueKey()); //生成shopID
        try {
           shopApplyService.save(shopApply);
        }catch (FanZoneException e){
            log.error("【商家申请入驻】保存失败",e);
            return ResultVOUntil.error(1,e.getMessage());
        }
        map.put("success","申请成功");
        return ResultVOUntil.success(map);
    }
}
